//
//  LGPatientDetailController.m
//  BreathTrainingTool
//
//  Created by 1234 on 2024/5/20.
//

#import "LGPatientDetailController.h"
//体征数据
#import "LGBodayStatisticsController.h"
//患者评估
#import "LGPatientEvaluateController.h"
//健康记录 - 服药统计
#import "LGPatientMedicineController.h"
//最近评估-详情
#import "LGEvaluateDetailController.h"
//患者报告
#import "LGPatientReportController.h"
//呼吸训练统计
#import "LGHistoryPrescriptionController.h"

#import "LGPatientDetailHeader.h"
/*  各类Cell  */
//健康档案
#import "LGPatientDetailHealthCell.h"
//近期血氧
#import "LGBodayStatisticsOxygenView.h"
#import "LGChartDetailAlertView.h"
//最近评估
#import "LGPatientDetailEvaluateCell.h"
//服药统计
#import "LGPatientDetailMedicCell.h"
//患者自传报告
#import "LGPatientDetailReportCell.h"
@interface LGPatientDetailController ()<UITableViewDelegate,UITableViewDataSource,LGPatientDetailHeaderDelegate,LGBodayStatisticsOxygenDelegate,LGPatientDetailMedicDelegate>
/**   */
@property (nonatomic , strong)LGPatientDetailHeader *headerView;
/**   */
@property (nonatomic , strong)UITableView *tableView;
/**   */
@property (nonatomic , strong)NSArray *titleArray;

@property (nonatomic , strong)NSDateFormatter *formatter;

/**  健康档案 - 展开、收起 */
@property (nonatomic , strong)NSArray *healthArray;
@property (nonatomic , strong)UIButton *healthBtn;
@property (nonatomic , assign)BOOL healthOpen;

/**  体征数据 */
@property (nonatomic , strong)LGChartDetailAlertView *alertView;
@property (nonatomic , strong)LGBodayStatisticsOxygenView *oxygenView;
@property (nonatomic , strong)UIButton *bodayButton;
@property (nonatomic , strong)NSArray *oxygenArray;
@property (nonatomic , copy)NSString *startDate30;
@property (nonatomic , copy)NSString *startDate7;
@property (nonatomic , copy)NSString *endDate;

/**  最近评估 */
@property (nonatomic , strong)UIButton *evaButton;
@property (nonatomic , strong)NSArray *evaArray;

/*  服药情况  */
@property (nonatomic , strong)UIButton *medicineBtn;
@property (nonatomic , strong)NSArray *medicinArray;
@property (nonatomic , strong)NSDictionary *medicinDic;
@property (nonatomic , assign)NSInteger medicinIndex;

/**  自传报告 */
@property (nonatomic , strong)UIButton *reportButton;
@property (nonatomic , strong)NSArray *reportArray;

@end

@implementation LGPatientDetailController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.titleArray = @[@"健康档案",@"最近呼吸肌力检测",@"结节信息",@"近期血氧心率",@"最近评估",@"进行中的执行处方",@"服药情况",@"患者近期报告"];
    self.endDate = [self.formatter stringFromDate:[NSDate date]];
    self.startDate7 = [self.formatter stringFromDate:[[NSDate date] initWithTimeIntervalSinceNow:-24*60*60*6]];
    self.startDate30 = [self.formatter stringFromDate:[[NSDate date] initWithTimeIntervalSinceNow:-24*60*60*29]];
    [self createSubView];
    [self requestHealthData];
    [self requestBloodOxygenData:self.startDate7];
    [self requestEvaluateData];
    [self requestMedicineData];
    [self requestReportData];
}

-(void)setType:(NSString *)type{
    _type = type;
    self.headerView.type = type;
    CGFloat headH = [type isEqualToString:@"inside"]?viewPix(188):viewPix(158);
    self.headerView.frame = CGRectMake(0, 0, Screen_W, headH);
    self.tableView.tableHeaderView = self.headerView;
}

-(void)requestHealthData{
    [RequestUtil withGET:@"/brbot-scale/answer/list" parameters:@{@"patientId":self.patientid,@"scaleId":@"3",@"batchNo":@"3"} header:YES success:^(NSInteger code, NSString *message, id returnData) {
        if (code == 200) {
            NSArray *returnArray = returnData;
            NSMutableArray *dataArray = [NSMutableArray array];
            for (NSDictionary *dataDic in returnArray) {
                NSMutableDictionary *tempDic = [NSMutableDictionary dictionary];
                [tempDic setValue:LGNSString(dataDic[@"scaleInfo"][@"questionTitle"]) forKey:@"title"];
                NSArray *answers = dataDic[@"answers"];
                NSMutableArray *idArray = [NSMutableArray array];
                for (NSDictionary *answerDic in answers) {
                    [idArray addObject:LGNSString(answerDic[@"id"])];
                }
                NSMutableArray *optionArray = [NSMutableArray array];
                NSString *option = LGNSString(dataDic[@"scaleInfo"][@"questionOption"]);
                if (option.length == 0) {
                    NSString *unit = LGNSString(dataDic[@"scaleInfo"][@"unitName"]);
                    for (NSDictionary *answerDic in answers) {
                        [optionArray addObject:[NSString stringWithFormat:@"%@%@",answerDic[@"freeText"],unit]];
                    }
                    
                    [tempDic setValue:optionArray forKey:@"items"];
                    
                }else{
                    
                    NSArray *options = [ObjectTool jsonToObject:option];
                    for (NSDictionary *optionDic in options) {
                        NSString *optionid = LGNSString(optionDic[@"id"]);
                        if ([idArray containsObject:optionid]) {
                            [optionArray addObject:LGNSString(optionDic[@"text"])];
                        }
                    }
                    [tempDic setValue:optionArray forKey:@"items"];
                }
                [dataArray addObject:tempDic];
            }
            self.healthArray = dataArray;
            [self.tableView reloadData];
        }
    } error:^(NSError *error) {
        
    }];
}

//血氧数据
-(void)requestBloodOxygenData:(NSString *)startDate{
    [RequestUtil withGET:@"/record/list" parameters:@{@"patientId":self.patientid,@"startDay":startDate,@"endDay":self.endDate,@"markType":@"33"} header:YES success:^(NSInteger code, NSString *message, id returnData) {
        if (code == 200) {
            self.oxygenView.dataArray = returnData;
        }
    } error:^(NSError *error) {
        
    }];
}

//最近评估
-(void)requestEvaluateData{
    [RequestUtil withGET:@"brbot-scale/answer/getScaleAnswerByGroup" parameters:@{@"patientId":self.patientid} header:YES success:^(NSInteger code, NSString *message, id returnData) {
        if (code == 200) {
            self.evaArray = returnData;
            [self.tableView reloadData];
        }
    } error:^(NSError *error) {
        
    }];
}

//请求服药情况
-(void)requestMedicineData{
    [RequestUtil withGET:@"/cure/medicineScheme/list" parameters:@{@"patientId":self.patientid} header:YES success:^(NSInteger code, NSString *message, id returnData) {
        if (code == 200) {
            NSArray *tempArray = returnData;
            if (tempArray.count>0) {
                self.medicinIndex = 0;
                NSString *schemaid = LGNSString(tempArray[0][@"id"]);
                [self requestMedicineChat:schemaid index:0];
            }
            self.medicinArray = tempArray;
        }
    } error:^(NSError *error) {
        
    }];
}

-(void)startDateWithDistance:(NSInteger)distance{
    NSDate *startDate = [[NSDate date] initWithTimeIntervalSinceNow:-24*60*60*distance];
    NSString *startDay = [self.formatter stringFromDate:startDate];
}

//请求服药统计图
-(void)requestMedicineChat:(NSString *)schemaid index:(NSInteger)index{
    self.medicinIndex = index;
    [RequestUtil withGET:@"/medicineTask/chart" parameters:@{@"schemaId":schemaid,@"patientId":self.patientid,@"startDay":self.startDate30,@"endDay":self.endDate} header:YES success:^(NSInteger code, NSString *message, id returnData) {
        if (code == 200) {
            self.medicinDic = returnData;
            [self.tableView reloadData];
        }
    } error:^(NSError *error) {
        
    }];
}

//患者自传报告
-(void)requestReportData{
    [RequestUtil withGET:@"/record/near" parameters:@{@"patientId":self.patientid} header:YES success:^(NSInteger code, NSString *message, id returnData) {
        if (code == 200) {
            self.reportArray = returnData;
            [self.tableView reloadData];
        }
    } error:^(NSError *error) {
        
    }];
}

//标记、取消标记 肺结节
-(void)updateTuberStatus:(NSString *)status{
    __block NSMutableDictionary *tempDic = [NSMutableDictionary dictionaryWithDictionary:self.patientDic];
    [tempDic setValue:status forKey:@"ifTuber"];
    
    [RequestUtil bodayPOST:@"/patient/markTubser" parameters:@{@"ifTuber":status,@"id":self.patientid} header:YES success:^(NSInteger code, NSString *message, id returnData) {
        if (code == 200) {
            self.patientDic = tempDic;
        }
    } error:^(NSError *error) {
        
    }];
}

-(void)setPatientDic:(NSDictionary *)patientDic{
    _patientDic = patientDic;
    self.headerView.dataDic = patientDic;
}


//展开、收起
-(void)healthButtonAction:(UIButton *)sender{
    sender.selected = !sender.selected;
    self.healthOpen = sender.selected;
    [self.tableView reloadSections:[NSIndexSet indexSetWithIndex:0] withRowAnimation:UITableViewRowAnimationFade];
}

//血氧
-(void)oxygenDayButtonAction:(UIButton *)sender{
    sender.selected = YES;
    if (sender == self.oxygenView.dayButton7) {
        self.oxygenView.dayButton30.selected = NO;
        [self requestBloodOxygenData:self.startDate7];
    }else{
        self.oxygenView.dayButton7.selected = NO;
        [self requestBloodOxygenData:self.startDate30];
    }
}

//更多体征数据
-(void)bodayStaticsButtonAction{
    LGBodayStatisticsController *controller = [[LGBodayStatisticsController alloc]init];
    controller.patientid = self.patientid;
    [self.navigationController pushViewController:controller animated:YES];
}

//查看全部评估
-(void)evaluateButtonAction{
    LGPatientEvaluateController *controller = [[LGPatientEvaluateController alloc]init];
    controller.patientid = self.patientid;
    [self.navigationController pushViewController:controller animated:YES];
}

//服药情况 - 更多健康记录
-(void)medicineButtonAction{
    LGPatientMedicineController *controller = [[LGPatientMedicineController alloc]init];
    controller.patientid = self.patientid;
    controller.medicineArray = self.medicinArray;
    [self.navigationController pushViewController:controller animated:YES];
}

//查看全部报告
-(void)reportButtonAction{
    LGPatientReportController *controller = [[LGPatientReportController alloc]init];
    controller.patientid = self.patientid;
    [self.navigationController pushViewController:controller animated:YES];
}

//查看历史处方详情
-(void)checkHistoryPrescription{
    LGHistoryPrescriptionController *controller = [[LGHistoryPrescriptionController alloc]init];
    controller.patientid = self.patientid;
    [self.navigationController pushViewController:controller animated:YES];
}


-(void)showAlertView:(NSArray *)dataArray  date:(NSString *)dateString  superPoint:(CGPoint)point{
    CGFloat marginX = (point.x>Screen_W/2.0)?(point.x-viewPix(140)-3):(point.x+3);
    self.alertView.frame = CGRectMake(marginX, point.y+3, viewPix(140), viewPix(35)+viewPix(24)*dataArray.count);
    self.alertView.dateString = dateString;
    self.alertView.dataArray = dataArray;
    self.alertView.hidden = NO;
    if(point.x>Screen_W/2.0){
        UIBezierPath *path = [UIBezierPath bezierPathWithRoundedRect:self.alertView.bounds byRoundingCorners:UIRectCornerTopLeft | UIRectCornerBottomLeft|UIRectCornerBottomRight cornerRadii:CGSizeMake(15, 15)];
        self.alertView.cornerLayer.path = path.CGPath;
    }else{
        UIBezierPath *path = [UIBezierPath bezierPathWithRoundedRect:self.alertView.bounds byRoundingCorners: UIRectCornerTopRight|UIRectCornerBottomLeft|UIRectCornerBottomRight cornerRadii:CGSizeMake(15, 15)];
        self.alertView.cornerLayer.path = path.CGPath;
    }
}

-(void)chartViewTouchesBegan{
    self.alertView.hidden = YES;
}

-(void)scrollViewDidScroll:(UIScrollView *)scrollView{
    [self chartViewTouchesBegan];
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return self.titleArray.count;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (section == 0) {
        //健康档案
        NSInteger closeCount = self.healthArray.count>0?1:0;
        return (self.healthOpen == YES)?self.healthArray.count:closeCount;
        
    }else if (section == 4 && self.evaArray.count>0){
        //最近评估
        return (self.evaArray.count>2)?2:self.evaArray.count;
        
    }else if (section == 6 && self.medicinArray.count>0){
        //服药统计图
        return 1;
    }else if (section == 7 && self.reportArray.count>0){
        //患者报告
        return self.reportArray.count;
    }
    return 0;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == 0) {
        //健康档案
        NSInteger closeCount = self.healthArray.count>0?1:0;
        NSInteger totalCount = (self.healthOpen == YES)?self.healthArray.count:closeCount;
        LGPatientDetailHealthCell *healthCell = [tableView dequeueReusableCellWithIdentifier:@"healthCell" forIndexPath:indexPath];
        healthCell.selectionStyle = UITableViewCellSelectionStyleNone;
        healthCell.dataDic = self.healthArray[indexPath.row];
        healthCell.type = (indexPath.row<totalCount-1)?@"middle":@"bottom";
        healthCell.tableView = self.tableView;
        return healthCell;
        
    }else if (indexPath.section == 4 && self.evaArray.count>0){
        //最近评估
        NSInteger totalCount = (self.evaArray.count>2)?2:self.evaArray.count;
        LGPatientDetailEvaluateCell *evaCell = [tableView dequeueReusableCellWithIdentifier:@"evaluateCell" forIndexPath:indexPath];
        evaCell.selectionStyle = UITableViewCellSelectionStyleNone;
        evaCell.type = (totalCount>1&&indexPath.row<totalCount-1)?LGPositionTypeMiddle:LGPositionTypeBottom;
        evaCell.dataDic = self.evaArray[indexPath.row];
        evaCell.tableView = self.tableView;
        return evaCell;
        
    }else if (indexPath.section == 6){
        //服药统计图
        LGPatientDetailMedicCell *medicinCell = [tableView dequeueReusableCellWithIdentifier:@"medicinCell" forIndexPath:indexPath];
        medicinCell.selectionStyle = UITableViewCellSelectionStyleNone;
        medicinCell.currentIndex = self.medicinIndex;
        medicinCell.medicineArray = self.medicinArray;
        medicinCell.dataDic = self.medicinDic;
        medicinCell.delegate = self;
        return medicinCell;
    }else if (indexPath.section == 7 && self.reportArray.count>0){
        //患者报告
        NSInteger totalCount = (self.reportArray.count>2)?2:self.reportArray.count;
        LGPatientDetailReportCell *reportCell = [tableView dequeueReusableCellWithIdentifier:@"reportCell" forIndexPath:indexPath];
        reportCell.selectionStyle = UITableViewCellSelectionStyleNone;
        reportCell.type = (totalCount>1&&indexPath.row<totalCount-1)?LGPositionTypeMiddle:LGPositionTypeBottom;
        reportCell.dataDic = self.reportArray[indexPath.row];
        reportCell.tableView = self.tableView;
        return reportCell;
    }
    return nil;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == 4) {
        NSDictionary *tempDic = self.evaArray[indexPath.row];
        LGEvaluateDetailController *controller = [[LGEvaluateDetailController alloc]init];
        controller.scaleid = LGNSString(tempDic[@"scaleId"]);
        controller.batchid = LGNSString(tempDic[@"batchNo"]);
        controller.bizid = LGNSString(tempDic[@"scaleIdByDicBizId"]);
        controller.time = LGNSString(tempDic[@"createTime"]);
        controller.score = LGNSString(tempDic[@"score"]);
        controller.patientid = self.patientid;
        [self.navigationController pushViewController:controller animated:YES];
    }
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    if ([self showContent:section] == YES) {
        return viewPix(52);
    }
    return 0.01;
}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    CGFloat height = ([self showContent:section] == YES)?viewPix(52):0.01;
    LGCornerHeaderView *headView = [[LGCornerHeaderView alloc]initWithFrame:CGRectMake(0, 0, Screen_W, height)];
    headView.topMargin = viewPix(8);
    UILabel *titleLabel = [UILabel lableWithText:self.titleArray[section] colorString:@"#2B2A37" textFont:LGFontWeight(16, UIFontWeightMedium) textAlignment:NSTextAlignmentLeft lines:1];
    titleLabel.frame = CGRectMake(viewPix(32), viewPix(20), Screen_W-viewPix(150), viewPix(22));
    [headView addSubview:titleLabel];
    
    if (section == 0 && self.healthArray.count > 0) {
        //健康档案
        [headView addSubview:self.healthBtn];
        
    }else if (section == 3){
        //体征数据
        [headView addSubview:self.bodayButton];
        
    }else if (section == 4 && self.evaArray.count>0){
        //最近评估
        [headView addSubview:self.evaButton];
        
    }else if (section == 6 && self.medicinArray.count>0){
        //服药情况
        [headView addSubview:self.medicineBtn];
        
    }else if (section == 7 && self.reportArray.count>0){
        //患者报告
        [headView addSubview:self.reportButton];
    }
    return headView;
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    CGFloat height = (section == 3)?viewPix(320):0.01;
    return height;
}

-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    CGFloat height = (section == 3)?viewPix(320):0.01;
    UIView *footView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, Screen_W, height)];
    footView.clipsToBounds = YES;
    if (section == 3) {
        [footView addSubview:self.oxygenView];
    }
    return footView;
}

-(BOOL)showContent:(NSInteger)section{
    if (section == 0 && self.healthArray.count > 0) {
        //健康档案
        return YES;
        
    }else if (section == 4 && self.evaArray.count>0){
        //最近评估
        return YES;
        
    }else if (section == 6 && self.medicinArray.count>0){
        //服药统计图
        return YES;
        
    }else if (section == 7 && self.reportArray.count>0){
        //患者报告
        return YES;
    }else if (section == 3){
        //近期血氧
        return YES;
    }
    return NO;
}

#pragma mark
#pragma mark ====> 创建控件
-(void)createSubView{
    self.fd_prefersNavigationBarHidden = YES;
    self.navigationView.title = @"患者详情";
    self.navigationView.type = LGNavigationTypeWhite;
    self.navigationView.backgroundColor = [UIColor clearColor];
    self.view.backgroundColor = [UIColor colorWithString:@"#F3F1FC"];
    UIImageView *bgImageView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"患者详情-背景"]];
    bgImageView.frame = CGRectMake(0, 0, Screen_W, viewPix(304));
    [self.view addSubview:bgImageView];
    [self.view addSubview:self.navigationView];
    [self.view addSubview:self.tableView];
    [self.view addSubview:self.alertView];
    [self creatBottomView];
}

-(UITableView *)tableView{
    if(!_tableView){
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, topBarHeight, Screen_W, Screen_H-topBarHeight-viewPix(99)) style:UITableViewStyleGrouped];
        [_tableView registerClass:[LGPatientDetailHealthCell class] forCellReuseIdentifier:@"healthCell"];
        [_tableView registerClass:[LGPatientDetailEvaluateCell class] forCellReuseIdentifier:@"evaluateCell"];
        [_tableView registerClass:[LGPatientDetailMedicCell class] forCellReuseIdentifier:@"medicinCell"];
        [_tableView registerClass:[LGPatientDetailReportCell class] forCellReuseIdentifier:@"reportCell"];
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.rowHeight = UITableViewAutomaticDimension;
        _tableView.backgroundColor = [UIColor clearColor];
        _tableView.showsVerticalScrollIndicator = NO;
        
        _tableView.dataSource = self;
        _tableView.delegate = self;
    }
    return _tableView;
}

-(LGPatientDetailHeader *)headerView{
    if(!_headerView){
        _headerView = [[LGPatientDetailHeader alloc]initWithFrame:CGRectMake(0, 0, Screen_W, viewPix(200))];
        _headerView.type = self.type;
        _headerView.delegate = self;
    }
    return _headerView;
}

-(UIButton *)healthBtn{
    if(!_healthBtn){
        _healthBtn = [UIButton buttonWithTitle:@"展开" titleFont:LGFont(14) textColor:kThemeColor imageName:@"themDownTip18"];
        [_healthBtn setSelectTitle:@"收起" selectImage:@"themUpTip18"];
        _healthBtn.frame = CGRectMake(Screen_W-viewPix(92), viewPix(10), viewPix(60), viewPix(42));
        [_healthBtn addTarget:self action:@selector(healthButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _healthBtn;
}

-(UIButton *)bodayButton{
    if(!_bodayButton){
        _bodayButton = [UIButton buttonWithTitle:@"更多体征数据" titleFont:LGFont(14) textColor:@"#909399" imageName:@"grayTip16"];
        _bodayButton.frame = CGRectMake(Screen_W-viewPix(140), viewPix(10), viewPix(110), viewPix(42));
        _bodayButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentRight;
        [_bodayButton setButtonInsetsStyle:LGButtonInsetsStyleRight marginSpace:2];
        [_bodayButton addTarget:self action:@selector(bodayStaticsButtonAction) forControlEvents:UIControlEventTouchUpInside];
    }
    return _bodayButton;
}

-(UIButton *)evaButton{
    if(!_evaButton){
        _evaButton = [UIButton buttonWithTitle:@"查看全部" titleFont:LGFont(14) textColor:@"#909399" imageName:@"grayTip16"];
        _evaButton.frame = CGRectMake(Screen_W-viewPix(140), viewPix(10), viewPix(110), viewPix(42));
        _evaButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentRight;
        [_evaButton setButtonInsetsStyle:LGButtonInsetsStyleRight marginSpace:2];
        [_evaButton addTarget:self action:@selector(evaluateButtonAction) forControlEvents:UIControlEventTouchUpInside];
    }
    return _evaButton;
}

-(UIButton *)medicineBtn{
    if(!_medicineBtn){
        _medicineBtn = [UIButton buttonWithTitle:@"更多健康记录" titleFont:LGFont(14) textColor:@"#909399" imageName:@"grayTip16"];
        _medicineBtn.frame = CGRectMake(Screen_W-viewPix(140), viewPix(10), viewPix(110), viewPix(42));
        _medicineBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentRight;
        [_medicineBtn setButtonInsetsStyle:LGButtonInsetsStyleRight marginSpace:2];
        [_medicineBtn addTarget:self action:@selector(medicineButtonAction) forControlEvents:UIControlEventTouchUpInside];
    }
    return _medicineBtn;
}

-(UIButton *)reportButton{
    if(!_reportButton){
        _reportButton = [UIButton buttonWithTitle:@"查看全部报告" titleFont:LGFont(14) textColor:@"#909399" imageName:@"grayTip16"];
        _reportButton.frame = CGRectMake(Screen_W-viewPix(140), viewPix(10), viewPix(110), viewPix(42));
        _reportButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentRight;
        [_reportButton setButtonInsetsStyle:LGButtonInsetsStyleRight marginSpace:2];
        [_reportButton addTarget:self action:@selector(reportButtonAction) forControlEvents:UIControlEventTouchUpInside];
    }
    return _reportButton;
}

-(LGBodayStatisticsOxygenView *)oxygenView{
    if(!_oxygenView){
        _oxygenView = [[LGBodayStatisticsOxygenView alloc]initWithFrame:CGRectMake(viewPix(16), -viewPix(50), Screen_W-viewPix(32)-1, viewPix(370))];
        [_oxygenView.dayButton7 addTarget:self action:@selector(oxygenDayButtonAction:) forControlEvents:UIControlEventTouchUpInside];
        [_oxygenView.dayButton30 addTarget:self action:@selector(oxygenDayButtonAction:) forControlEvents:UIControlEventTouchUpInside];
        
        _oxygenView.delegate = self;
    }
    return _oxygenView;
}
-(LGChartDetailAlertView *)alertView{
    if(!_alertView){
        _alertView = [[LGChartDetailAlertView alloc] initWithFrame:CGRectMake(0, 0, viewPix(140), viewPix(50))];
        _alertView.hidden = YES;
    }
    return _alertView;
}

-(void)creatBottomView{
    UIView *bottomView = [[UIView alloc]initWithFrame:CGRectMake(0, kMaxY(self.tableView.frame), Screen_W, viewPix(150))];
    bottomView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:bottomView];
    
    UIButton *bottomBtn = [UIButton buttonWithTitle:@"训练统计详情" titleFont:LGFont(16) textColor:@"#796DF5" frame:CGRectMake(viewPix(16), viewPix(16), Screen_W-viewPix(32), viewPix(48))];
    [bottomBtn addBorder:@"#796DF5" lineWidth:1 cornerRidus:viewPix(24)];
    [bottomBtn addTarget:self action:@selector(checkHistoryPrescription) forControlEvents:UIControlEventTouchUpInside];
    [bottomView addSubview:bottomBtn];
}

-(NSDateFormatter *)formatter{
    if(!_formatter){
        _formatter = [[NSDateFormatter alloc]init];
        _formatter.dateFormat = @"yyyy-MM-dd";
    }
    return _formatter;
}

@end
